package org.dromara.legal.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.legal.domain.bo.ConsultBo;
import org.dromara.legal.domain.vo.ConsultVo;
import org.dromara.legal.mapper.ConsultMapper;
import org.dromara.legal.service.IConsultService;
import org.dromara.system.domain.bo.SysUserBo;
import org.dromara.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.dromara.legal.domain.bo.LawyerBo;
import org.dromara.legal.domain.vo.LawyerVo;
import org.dromara.legal.domain.Lawyer;
import org.dromara.legal.mapper.LawyerMapper;
import org.dromara.legal.service.ILawyerService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 律师Service业务层处理
 *
 * @author zhuqingrun
 * @date 2024-10-15
 */
@RequiredArgsConstructor
@Service
public class LawyerServiceImpl implements ILawyerService {

    private final LawyerMapper baseMapper;

    private final ConsultMapper consultMapper;

    private final ISysUserService userService;

    /**
     * 查询律师
     *
     * @param id 主键
     * @return 律师
     */
    @Override
    public LawyerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询律师列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 律师分页列表
     */
    @Override
    public TableDataInfo<LawyerVo> queryPageList(LawyerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Lawyer> lqw = buildQueryWrapper(bo);
        Page<LawyerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的律师列表
     *
     * @param bo 查询条件
     * @return 律师列表
     */
    @Override
    public List<LawyerVo> queryList(LawyerBo bo) {
        LambdaQueryWrapper<Lawyer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Lawyer> buildQueryWrapper(LawyerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Lawyer> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), Lawyer::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getOffice()), Lawyer::getOffice, bo.getOffice());
        lqw.eq(bo.getHiredate() != null, Lawyer::getHiredate, bo.getHiredate());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), Lawyer::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getAvatar()), Lawyer::getAvatar, bo.getAvatar());
        lqw.eq(StringUtils.isNotBlank(bo.getSkill()), Lawyer::getSkill, bo.getSkill());
        return lqw;
    }

    /**
     * 新增律师
     *
     * @param bo 律师
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(LawyerBo bo) {
        Lawyer add = MapstructUtils.convert(bo, Lawyer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            SysUserBo user = new SysUserBo();
            user.setNickName(add.getName());
            user.setUserName(add.getPhone());
            user.setPassword("123456");
            user.setPhonenumber(add.getPhone());
            Long [] roleIds = {5L};
            user.setRoleIds(roleIds);
            userService.insertUser(user);
        }
        return flag;
    }

    /**
     * 修改律师
     *
     * @param bo 律师
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(LawyerBo bo) {
        Lawyer update = MapstructUtils.convert(bo, Lawyer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Lawyer entity){

    }

    /**
     * 校验并批量删除律师信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            for (Long id : ids) {
                ConsultBo consultBo = new ConsultBo();
                consultBo.setLawyerId(id);
                List<ConsultVo> consultVos = consultMapper.selectVoList(new LambdaQueryWrapper<>());
                if (consultVos.size() > 0) {
                    throw new RuntimeException("律师："+ queryById(id).getName()+"存在咨询记录，无法删除！请重新选择");
                }
            }
        }
        boolean b = baseMapper.deleteByIds(ids) > 0;
        //userService.deleteUserById(ids);
        return b;
    }
}
